//
//  HMUserAccessViewModel.swift
//  swift微博项目
//
//  Created by huodian on 16/9/26.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit
let url = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("sear.plist")

class HMUserAccessViewModel: NSObject {

    static let shareUserAccess : HMUserAccessViewModel = HMUserAccessViewModel ()
    
    var userAccess :HMUserAccessModel?
    
    
    var isOnLoad : Bool  {
        
        if userAccess?.access_token != nil && isTimeOut == false {
            
            return true
        }
        return false
        
    }
    var isTimeOut : Bool {
     
        if  userAccess?.timeOut_in?.compare(Date()) == ComparisonResult.orderedAscending{
            
            return true
        }
        return false
        
        
        
    }
    
    override init() {
        
        super.init()
        
       self.userAccess = loadUserAccess()
        
    }
    
    
    func loadTheToken(code:String ,Finish:@escaping (Bool)->()) {
        //获取授权
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        //拼接参数
        let paramear = ["client_id" : client_id,
                        "client_secret" : client_secret,
                        "grant_type" : "authorization_code",
                        "code" : code,
                        "redirect_uri" : redirect_uri
        ]
        
        HMNetWorking.shareNetWorking.requset(method: .POST, url: urlStr, parameters: paramear) { (responsedObject, error ) in
            
            if error != nil {
                
                Finish(false)
                
                print(error)
                return
                
            }
            print("qibsbifs",responsedObject)
            
            //将finish作为参数回调成功或失败的结果
            self.loadUserMessage(dict: responsedObject as! [String:Any],Finish: Finish)
        }
        
        
        
    }
    
    func loadUserMessage(dict:[String:Any],Finish:@escaping (Bool)->())
    {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let access_token = dict["access_token"]!
        let uid = dict["uid"]!
        
        let paramar = ["access_token":access_token,
                       "uid":uid]
        
        HMNetWorking.shareNetWorking.requset(method: .GET, url: urlString, parameters: paramar) { (responsedObject, error) in
            
            if error != nil {
                
                Finish(false)
                return
                
            }
            
            
            
            var dic = responsedObject as! [String:Any]
            
            //拼接字典
            for dicValue in dict {
                
                dic[dicValue.key] = dicValue.value
                
                
            }
            
            print("qibsbifs",dict)
            let userAccess = HMUserAccessModel(dict: dic)
            
            self.userAccess = userAccess
            
            self.saveTheUserAccess(Model: userAccess)
            
            Finish(true)
            
        }
        
        
    }
    
    func saveTheUserAccess(Model:HMUserAccessModel){
        
        
        
        print(url)
        
        NSKeyedArchiver.archiveRootObject(Model, toFile: url)
        
        
    }
    
    func loadUserAccess()->(HMUserAccessModel){
        
        let useraccount = NSKeyedUnarchiver.unarchiveObject(withFile: url) as? HMUserAccessModel
        
        
        return useraccount!
        
    }
    
}
